package net.xdclass.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.xdclass.enums.ShortLinkStateEnum;
import net.xdclass.manager.GroupCodeMappingManager;
import net.xdclass.mapper.GroupCodeMappingMapper;
import net.xdclass.model.GroupCodeMappingDO;
import net.xdclass.vo.GroupCodeMappingVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Allen
 * @version 1.0
 * @date 2022/11/16 15:13
 */
@Component
public class GroupCodeMappingManagerImpl implements GroupCodeMappingManager {
	@Autowired
	private GroupCodeMappingMapper groupCodeMappingMapper;
	@Override
	public GroupCodeMappingDO findByGroupIdAndMappingId(Long mappingId, Long accountNo, Long groupId) {

		GroupCodeMappingDO groupCodeMappingDO = groupCodeMappingMapper.selectOne(new QueryWrapper<GroupCodeMappingDO>()
			.eq("id", mappingId).eq("account_no", accountNo)
			.eq("group_id", groupId));

		return groupCodeMappingDO;
	}

	@Override
	public int add(GroupCodeMappingDO groupCodeMappingDO) {
		return groupCodeMappingMapper.insert(groupCodeMappingDO);
	}

	@Override
	public int del(String shortLinkCode, Long accountNo, Long groupId) {

		int rows = groupCodeMappingMapper.update(null, new UpdateWrapper<GroupCodeMappingDO>()
			.eq("code", shortLinkCode).eq("account_no", accountNo)
			.eq("group_id", groupId).set("del", 1));

		return rows;
	}

	@Override
	public Map<String, Object> pageShortLinkByGroupId(Integer page, Integer size, Long accountNo, Long groupId) {

		Page<GroupCodeMappingDO> pageInfo = new Page<>(page, size);

		Page<GroupCodeMappingDO> groupCodeMappingDOPage = groupCodeMappingMapper.selectPage(pageInfo, new QueryWrapper<GroupCodeMappingDO>().eq("account_no", accountNo)
			.eq("group_id", groupId));

		Map<String, Object> pageMap = new HashMap<>(3);

		pageMap.put("total_record", groupCodeMappingDOPage.getTotal());
		pageMap.put("total_page", groupCodeMappingDOPage.getPages());
		pageMap.put("current_data", groupCodeMappingDOPage.getRecords()
			.stream().map(obj -> beanProcess(obj)).collect(Collectors.toList()));


		return pageMap;
	}

	@Override
	public int updateGroupCodeMappingState(Long accountNo, Long groupId, String shortLinkCode, ShortLinkStateEnum shortLinkStateEnum) {

		int rows = groupCodeMappingMapper.update(null, new UpdateWrapper<GroupCodeMappingDO>()
			.eq("code", shortLinkCode).eq("account_no", accountNo)
			.eq("group_id", groupId).set("state", shortLinkStateEnum.name()));

		return rows;
	}


	private GroupCodeMappingVO beanProcess(GroupCodeMappingDO groupCodeMappingDO) {
		GroupCodeMappingVO groupCodeMappingVO = new GroupCodeMappingVO();
		BeanUtils.copyProperties(groupCodeMappingDO, groupCodeMappingVO);

		return groupCodeMappingVO;
	}
}
